Eddie's Lessons
Beginners Programming
Using a filedialog to find Liberty.exe
Eddie now allows users to run code written in Eddie by invoking Liberty BASIC. Eddie needs to know where to find the Liberty BASIC executable file, called liberty.exe. To get the location of liberty.exe, we could ask the user to type the path to liberty.exe into a prompt box. If he makes a mistake, our program won't be able to run the code, so that isn't the best idea. We could use an API call to search for liberty.exe, but if the user has a lot of files on his hard drive, it goes rather slowly. We could also do a recursive file search using native LB code, but that would be very complicated! The easiest way to get the location of liberty.exe is to present the user with a filedialog and allow him to navigate to the correct folder and select liberty.exe.
Filedialog Syntax
The filedialog command is followed by a string or a string variable containing the caption. The caption appears on the titlebar of the filedialog. Next is the template, which is a literal string or a string variable that specifies which files or filetypes show in the filedialog listbox. After the filedialog closes, the receiver variable holds the name of the file selected by the user.
filedialog "Caption","template string", receiverVariable$
A filedialog looks like this:

Disk Filename Extensions
The "template string" contains a list of file names or extensions that will be displayed in the file listbox in the filedialog. The part of a filename after the dot is called the extension. There are many standard extensions in use. Liberty BASIC and other BASIC languages use the extension BAS. Liberty BASIC also creates backup files. The standard extension for a backup file is BAK. Text files have an extension of TXT. Word documents are DOC. Bitmap images have an extension of BMP.
To cause a filedialog to show all fileames, use the template "*.*" The asterisk represents one or more optional characters. This is called a wildcard.
filedialog "Open","*.*",filename$
To show only text files:
filedialog "Open","*.txt",filename$
The name can be included as well. To show only files called "readme.txt":
filedialog "Open","readme.txt",filename$
The template string can also include drive or folder information, like this:
filedialog "Open","c:\*.txt",filename$
Multiple File Extensions
The template string can hold multiple file extensions, if they are separated by semicolons. To find all files that have the extension BAS or TXT, use this:
filedialog "Open","*.bas;*.txt",filename$
Finding Liberty.Exe
Here's the code from Eddie that allows the user to find liberty.exe:
filedialog "Find Liberty.exe","*liberty*.exe",LibertyExe$
To make it even easier for the user, we've made the template string include "liberty.exe" so only files that match that name show in the listbox in the filedialog. We've added some asterisk characters to the template string, so any executable file that contains "liberty" will show. A filename called "libertyHello.exe" would be displayed, for instance, as would one called "MyLiberty.exe."

The Receiver Variable
When the user dismisses the filedialog, the receiver variable contains the name of the file he selected. The receiver variable here is called LibertyExe$.
Error Trapping
What happens if the user cancels the filedialog without selecting liberty.exe? Eddie needs to know that, so he doesn't try to run the code in the editor. If the user cancels a filedialog, the returned filname is an empty string, or "". Let's write some error-trapping code:
if LibertyExe$="" then
'the user cancelled the filedialog
notice "Liberty.exe not found."
return
end if
Eddie's Lessons
Beginners Programming